package com.qys.livesMall.shop.controller.app;

import com.qys.livesMall.app.user.service.IH2AppUserService;
import com.qys.livesMall.common.exception.QysException;
import com.qys.livesMall.common.exception.enums.ExceptionEnum;
import com.qys.livesMall.common.message.MessageServer;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.framework.view.QysPage;
import com.qys.livesMall.shop.modal.dto.shop.rw.CancelRw;
import com.qys.livesMall.shop.modal.dto.shop.rw.EditRw;
import com.qys.livesMall.shop.modal.dto.shop.rw.ShopRwSearch;
import com.qys.livesMall.shop.modal.vo.ShopRechargeWithdrawalVO;
import com.qys.livesMall.shop.service.H2ShopBaseService;
import com.qys.livesMall.shop.service.H2ShopRechargeWithdrawalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * app-店铺充值提现
 *
 * @author
 * @since 2023-06-06
 */
@RestController
@RequestMapping("/app/shop/rw")
public class AppH2ShopRechargeWithdrawalController {

    @Autowired
    private H2ShopRechargeWithdrawalService rechargeWithdrawalService;

    @Autowired
    private H2ShopBaseService h2ShopBaseService;

    @Autowired
    private MessageServer messageServer;

    @Autowired
    private IH2AppUserService appUserService;

    /**
     * 充值提现列表
     * @param rwSearch
     * @return
     */
    @PostMapping("/list")
    public R<QysPage<ShopRechargeWithdrawalVO>> search(@RequestBody ShopRwSearch rwSearch){
        rwSearch.setUserId(SecurityUtils.getUserId());
        return R.ok(rechargeWithdrawalService.search(rwSearch));
    }

    /**
     * 充值
     * @param rw
     * @return
     */
    @PostMapping("/recharge")
    public R recharge(@RequestBody EditRw rw){
        return rechargeWithdrawalService.recharge(rw);
    }

    /**
     * 提现
     * @param rw
     * @return
     */
    @PostMapping("/withdrawal")
    public R withdrawal(@RequestBody EditRw rw){
        if (rw==null){
            return messageServer.failMsg(ExceptionEnum.PARAM_IS_ERROR);
        }
        //检查密码
        boolean res=appUserService.checkUserPayPassword(SecurityUtils.getUserId().toString(),rw.getPassword());
        if (!res){
            return R.fail();
        }
        return rechargeWithdrawalService.withdrawal(rw);
    }

    /**
     * 取消
     * @param cancelRw
     * @return
     */
    @PostMapping("/cancel")
    public R cancel(@RequestBody CancelRw cancelRw){
        return rechargeWithdrawalService.cancel(cancelRw);
    }

}
